﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Diagnostics;

namespace Zero.UControl.Demo.Views.UAdorner.Component
{
    public class RotateAdorner : ElementDecoratorBase
    {
        private Thumb _rotateThumb;
        private VisualCollection _visuals;
        private Point _centerPoint;
        private double _startAngle;       // 拖动开始时的初始角度

        public RotateAdorner(UIElement adornedElement) : base(adornedElement)
        {
            _visuals = new VisualCollection(this);

            _rotateThumb = new Thumb
            {
                Width = 10,
                Height = 10,
                Background = Brushes.Red,
                Cursor = Cursors.Hand
            };

            _rotateThumb.DragStarted += RotateThumb_DragStarted;
            _rotateThumb.DragDelta += RotateThumb_DragDelta;
            _visuals.Add(_rotateThumb);
        }

        public override void Attach(UIElement element, Canvas canvas)
        {
            base.Attach(element, canvas);
        }

        private void RotateThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            if (AdornedElement is FrameworkElement fe)
            {
                TransformUtil.EnsureTransformGroup(fe);
                var rotate = TransformUtil.GetRotate(fe);

                // 元素中心点（Adorner坐标系）
                _centerPoint = fe.TranslatePoint(new Point(fe.Width / 2, fe.Height / 2), this);

                // 当前鼠标相对于中心点的初始向量
                Point mousePos = Mouse.GetPosition(this);
                Vector startVector = mousePos - _centerPoint;

                // 起始角度
                _startAngle = Math.Atan2(startVector.Y, startVector.X) * 180 / Math.PI;

                // 修正为上方为 0 度
                _startAngle -= 90;
            }
        }

        private void RotateThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (AdornedElement is FrameworkElement fe)
            {
                var _rotate = TransformUtil.GetRotate(fe);
                Point mousePos = Mouse.GetPosition(this);
                Vector currentVector = mousePos - _centerPoint;

                double currentAngle = Math.Atan2(currentVector.Y, currentVector.X) * 180 / Math.PI;
                currentAngle -= 90;

                // 计算角度差值
                double deltaAngle = currentAngle - _startAngle;
           
                // 应用差值（累加到原角度）
                _rotate.Angle += deltaAngle;

                // 更新起始角度为当前角度，防止积累误差
                _startAngle = currentAngle;

                // 手动触发布局更新,通知其他的装饰器进行变化
                this.InvalidateVisual(); // 重绘当前 RotateAdorner 自己
            }
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (AdornedElement is FrameworkElement fe)
            {
                _rotateThumb.Arrange(new Rect(fe.ActualWidth / 2 - 5,-10,10,10));
            }
            return finalSize;
        }


        protected override int VisualChildrenCount => _visuals.Count;
        protected override Visual GetVisualChild(int index) => _visuals[index];
    }

}
